package com.millstein.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.millstein.commonutils.Result;
import com.millstein.eduservice.entity.EduTeacher;
import com.millstein.eduservice.entity.vo.TeacherQuery;
import com.millstein.eduservice.service.EduTeacherService;
import com.millstein.servicebase.handler.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-06-29
 */
@Api(tags="讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    /**
     * 查询全部
     * @return
     */
    @GetMapping("findAll")
    @ApiOperation(value = "所有讲师列表")
    public Result findAll() {
        return Result.ok().data("items", eduTeacherService.list(null));
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    @ApiOperation(value = "根据ID删除讲师")
    public Result delete(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable("id") String id) {
        return eduTeacherService.removeById(id) == true ? Result.ok() : Result.error();
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("pageTeacher/{current}/{limit}")
    public Result pageListTeacher(@PathVariable("current") Integer page, @PathVariable("limit") Integer pageSize) {
        Page<EduTeacher> pageTeacher = new Page<>(page, pageSize);
        // 调用方法后，所有分页的数据都会放到Page对象中去
        eduTeacherService.page(pageTeacher, null);

        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return Result.ok().data("total", total).data("rows", records);
    }

    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public Result pageTeacherCondition(@PathVariable("current") Integer page,
                                       @PathVariable("limit") Integer pageSize,
                                       @RequestBody TeacherQuery query) {
        Page<EduTeacher> teacherPage = new Page<>(page, pageSize);
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(query.getName())) {
            wrapper.like("name", query.getName());
        }
        if (!StringUtils.isEmpty(query.getLevel())) {
            wrapper.eq("level", query.getLevel());
        }
        if (!StringUtils.isEmpty(query.getBegin())) {
            wrapper.gt("gmt_create", query.getBegin());
        }
        if (!StringUtils.isEmpty(query.getEnd())) {
            wrapper.lt("gmt_create", query.getBegin());
        }
        eduTeacherService.page(teacherPage, wrapper);

        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();
        return Result.ok().data("total", total).data("rows", records);
    }

    /**
     * 添加讲师
     * @param eduTeacher
     * @return
     */
    @PostMapping("addTeacher")
    public Result addTeacher(@RequestBody EduTeacher eduTeacher) {
        return eduTeacherService.save(eduTeacher) ? Result.ok() : Result.error();
    }

    /**
     * 根据id查询讲述
     * @param id
     * @return
     */
    @GetMapping("getTeacher/{id}")
    public Result getTeacher(@PathVariable("id") String id) {
        throw new GuliException(20001, "起床搬砖了");
//        return Result.ok().data("teacher", eduTeacherService.getById(id));
    }

    /**
     * 修改讲师
     * @param eduTeacher
     * @return
     */
    @PutMapping("updateTeacher")
    public Result updateTeacher(@RequestBody EduTeacher eduTeacher) {
        return eduTeacherService.updateById(eduTeacher) ? Result.ok() : Result.error();
    }
}

